<script setup lang="ts">
import CustomNavbar from './components/CustomNavbar.vue'
import CategoryPanel from './components/CategoryPanel.vue'
import { getHomeBannerAPI, getHomeCategoryAPI, getHomeHotMutliAPI } from '@/services/home'
import type { HomeBannerItem, HomeCategoryItem, HomeHotMutliItem } from '@/types/home'
import { ref } from 'vue'
import HotPanel from './components/HotPanel.vue'
import PageSkeleton from './components/PageSkeleton.vue'
import { onLoad } from '@dcloudio/uni-app'
import { useGuessList } from '@/composables'

// 首页广告区域
const bannerList = ref<HomeBannerItem[]>([])
const getHomeBanner = async () => {
  const res = await getHomeBannerAPI(1)
  // console.log('首页广告区域', res)
  bannerList.value = res.result
}

// 首页前台分类
const categoryList = ref<HomeCategoryItem[]>([])
const getHomeCategory = async () => {
  const res = await getHomeCategoryAPI()
  // console.log('首页前台分类', res)
  categoryList.value = res.result
}

// 首页热门推荐
const hotList = ref<HomeHotMutliItem[]>([])
const getHomeHotMutli = async () => {
  const res = await getHomeHotMutliAPI()
  // console.log('首页热门推荐', res)
  hotList.value = res.result
}

// 是否加载中标记
const isLoading = ref(false)

//页面加载
onLoad(async () => {
  // 开启骨架屏
  isLoading.value = true
  // 请求数据
  await Promise.all([getHomeBanner(), getHomeCategory(), getHomeHotMutli()])
  // 关闭骨架屏
  isLoading.value = false
})

//猜你喜欢的组合式函数
const { guessRef, onScrolltolower } = useGuessList()

// 下拉刷新状态
const isTriggered = ref(false)

// 自定义下拉刷新
const onRefresherrefresh = async () => {
  // 开启动画
  isTriggered.value = true
  // 重置数据
  guessRef.value?.resetData()
  // 请求数据
  await Promise.all([
    getHomeBanner(),
    getHomeCategory(),
    getHomeHotMutli(),
    guessRef.value?.getMore(),
  ])
  // 关闭动画
  isTriggered.value = false
}
</script>

<template>
  <!-- 头部导航 -->
  <CustomNavbar />
  <!-- 滚动容器 -->
  <scroll-view
    refresher-enabled
    :refresher-triggered="isTriggered"
    @refresherrefresh="onRefresherrefresh"
    @scrolltolower="onScrolltolower"
    class="scroll-view"
    scroll-y
  >
    <PageSkeleton v-if="isLoading" />
    <!-- 轮播图 -->
    <XtxSwiper :banner-list="bannerList" />
    <!-- 分类 -->
    <CategoryPanel :category-list="categoryList" />
    <!-- 热门推荐 -->
    <HotPanel :hot-list="hotList" />
    <!-- 猜你喜欢 -->
    <XtxGuess ref="guessRef" />
  </scroll-view>
</template>

<style lang="scss">
// 滚动容器
.scroll-view {
  height: 100vh;
}
</style>
